// ReadConfig.cpp: implementation of the CReadConfig class.
//
//////////////////////////////////////////////////////////////////////

#include "ReadConfig.h"
#include "ace/INET_Addr.h"
#include "GlobalSetting.h"
#include "../include/proto.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CReadConfig* CReadConfig::instance_ = 0;

CReadConfig::CReadConfig()
{
}

CReadConfig::~CReadConfig()
{
}

int CReadConfig::ReadIniConfig()
{
	if ((cfg.open ()) != 0)
		ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Cannot open cfg1\n")),	-1);

	root = cfg.root_section();
	ACE_Ini_ImpExp importExport (cfg);

	ACE_TCHAR iniLocation[_MAX_PATH] = "WTVS5.ini";

	int value_int;
	//GetModuleFileName(NULL, iniLocation, _MAX_PATH);
	//(ACE_OS::strrchr(iniLocation,'\\'))[1] = 0; 
	//ACE_OS::strcat(iniLocation, ACE_TEXT ("WTVS5.ini")); 


	int rc = importExport.import_config (iniLocation);
	if (rc != 0)
		ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Error Importing (%d)\n"), rc), -1);


	ACE_TCHAR str[TEST_MAX_STRING] = {0};
	if (cfg.open_section (root,	ACE_TEXT ("StreamServer"), 1, ServerSection))
		return -1;

	get_section_integer(cfg, ServerSection, ACE_TEXT ("ServerPort_Client"), &value_int, 1, 65535);
	GlobalSetting::instance()->liveStreamPort = (u_short)value_int;

	get_section_integer(cfg, ServerSection, ACE_TEXT ("ServerPort_Encorder"), &value_int, 1, 65535);
	GlobalSetting::instance()->StreamPort = (u_short)value_int;

	get_section_string(cfg, ServerSection, ACE_TEXT ("ProxyServerList"), str, TEST_MAX_STRING);
	ACE_CString cstr = str;
	StringToVec(cstr);


	return 0;
}

int CReadConfig::WriteIniConfig()
{
	if ((cfg.open ()) != 0)
		ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Cannot open cfg1\n")),	-1);

	root = cfg.root_section();


	if (cfg.open_section (root,	ACE_TEXT ("Server"), 1, ServerSection))
		return -1;

	ACE_CString str;
	VecToString(str);
	if (cfg.set_string_value (ServerSection, ACE_TEXT ("ServerIP"), str ))
		return -1;

	ACE_Ini_ImpExp importExport (cfg);

	ACE_TCHAR iniLocation[_MAX_PATH] = "ipc.ini";
	//GetModuleFileName(NULL, iniLocation, _MAX_PATH);
	//(ACE_OS::strrchr(iniLocation,'\\'))[1] = 0; 
	//ACE_OS::strcat(iniLocation, ACE_TEXT ("ipc.ini")); 

	int rc = importExport.export_config (ACE_TEXT (iniLocation));
	if (rc != 0)
		ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Error Exporting (%d)\n"), rc), -1);

	return 0;
}






// Reads a string value from a configuration object.

void
CReadConfig::get_section_string (ACE_Configuration&             config,
                                 ACE_Configuration_Section_Key& SectionKey,
                                 const ACE_TCHAR*               pszName,
                                 ACE_TCHAR*                     pszVariable,
                                 int                            nMaxLength)
{
  ACE_TString StringValue;

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszVariable,
                       StringValue.c_str (),
                       nMaxLength);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s = %s\n"),
                  pszName,
                  pszVariable));
    }
}

// Reads an integer value from a configuration object (when it's
// stored as a string)
void
CReadConfig::get_section_integer (ACE_Configuration&             config,
                                  ACE_Configuration_Section_Key& SectionKey,
                                  const ACE_TCHAR*               pszName,
                                  int*                           nVariable,
                                  int                            nMinValue,
                                  int                            nMaxValue)
{
  ACE_TString StringValue;
  ACE_TCHAR pszString[30];
  ACE_OS::strcpy (pszString, ACE_TEXT ("0"));
  int IntegerValue = 0;

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszString,
                       StringValue.c_str (),
                       30);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s = %s\n"),
                  pszName,
                  pszString));
    }

  // convert to integer
  IntegerValue = ACE_OS::atoi (pszString);
  IntegerValue = (IntegerValue < nMinValue) ? nMinValue : IntegerValue;
  IntegerValue = (IntegerValue > nMaxValue) ? nMaxValue : IntegerValue;
  *nVariable = IntegerValue;
}

// Reads a boolean value from a configuration object (when it's stored as a string).

void
CReadConfig::get_section_boolean (ACE_Configuration&             config,
                                  ACE_Configuration_Section_Key& SectionKey,
                                  const ACE_TCHAR*               pszName,
                                  int*                           pVariable)
{
  ACE_TString StringValue;
  ACE_TCHAR pszString[10];
  ACE_OS::strcpy (pszString, ACE_TEXT ("0"));

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszString,
                       StringValue.c_str (),
                       10);
      for (ACE_TCHAR* pSrc = pszString;
           *pSrc != ACE_TEXT ('\0');
           pSrc++)
        // Convert to uppercase
        if (islower (*pSrc))
          *pSrc = tolower (*pSrc);

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s = %s\n"),
                  pszName,
                  pszString));

      if (ACE_OS::strcmp (pszString,
                          ACE_TEXT ("TRUE")) == 0)
        *pVariable = 1;
      else if (ACE_OS::strcmp (pszString,
                               ACE_TEXT ("FALSE")) == 0)
        *pVariable = 0;
    }
}

void CReadConfig::StringToVec(ACE_CString str)
{
	char seps[] = ";";
	char *token=NULL;

	int i = 0;

	token = strtok((char*)(str.c_str()), seps);
	while( token != NULL ) {
		//ACE_INET_Addr tte("10.1.1.0:255");
		GlobalSetting::instance()->streamServ_vec.push_back(ACE_INET_Addr(token));
		token = strtok( NULL, seps );
	}

}

void CReadConfig::VecToString(ACE_CString &str)
{
}

